package com.yinpeng;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by yinpeng on 2020/06/28
 */
public class TestCglib {

	public static void main (String[] args) throws Exception{
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(Daxiang.class);
		enhancer.setCallback(new DaxiangMethodInterceptor());
		enhancer.setCallbackType(DaxiangMethodInterceptor.class);
		enhancer.setUseFactory(false);
		Object o = enhancer.create();
		Daxiang dx  = (Daxiang) o;
		dx.method();
	}
}

class Daxiang {

	public void method(){
		System.out.println("run method");
	}
}

class DaxiangMethodInterceptor implements MethodInterceptor{
	@Override
	public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
		System.out.println("before---");
		Object o1 = methodProxy.invokeSuper(o,objects);
		System.out.println("after---");
		return o1;
	}
}



